package com.rui.study.algorithm.P_二分查找计算平方根精确到6位小数;

import com.rui.study.algorithm.PrintUtil;

import java.text.DecimalFormat;

/**
 * @program: study
 * @description:
 * @author: Yaowr
 * @create: 2018-11-09 15:53
 **/
public class Solution {

    public static double sqrt(double n) {
        double start = 0;
        double end = n;
        double i = 0f;
        DecimalFormat df = new DecimalFormat("#.000000");
        while (end > start) {
            i = Double.valueOf(df.format((start + end) / 2));
            if (i * i > n) {
                end = i - 0.000001f;
            } else if (i * i < n) {
                start = i + 0.000001f;
            } else {
                return i;
            }
        }
        return i;
    }

    public static void main(String[] args) {
        PrintUtil.print(sqrt(10));

//        DecimalFormat df = new DecimalFormat("#.000000");
//        PrintUtil.print(df.format(7.0000005d));
    }
}
